"""
@Time ： 2023/7/18 0009 10:07
@Auth ： zpt
@File ：ConfigSystem.py
@IDE ：PyCharm
"""
import time

from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

from Page.LoginPage import LoginPage
from Lib.common_lib import LOG_INFO, LOG_ERROR


class ConfigSystem(object):
    """镜像定制页面"""
    product_name = "openEuler"
    config_kernel = "net.ifnames=0 biosdevname=0 crashkernel=512M"
    repo_url = "https://repo.openeuler.org/openEuler-22.03-LTS/everything/aarch64/"

    def __init__(self, driver):
        self.driver = driver

    def get_url(self, url):
        self.driver.get(url)

    def user_login(self, user_name, password):
        """
        用户登录
        """
        login = LoginPage(self.driver)
        login.user_login()
        login.login_valid(user_name, password)

    def config_system_button(self):
        """
        系统配置参数按钮
        """
        try:
            self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                               '2]/section/section/aside/div[3]/div[3]/div[1]').click()

        except Exception as e:
            message = '获取配置系统参数按钮'
            LOG_ERROR("error: found config system button timeout")
            print(e)

    def edit_button(self):
        """
        定制业务包页面修改按钮
        """
        try:
            self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                               '2]/section/section/main/div/div/div[1]/div[1]/button').click()
        except Exception as e:
            message = '修改按钮定位失败'
            LOG_ERROR("error: found edit button timeout")
            print(e)
            LOG_INFO(message)

    def save_button(self):
        """
        配置系统参数页面保存按钮
        """
        try:
            self.driver.find_element(By.XPATH, '//*[@id="right"]/div/div/div/div[1]/div[2]/button[1]').click()
        except Exception as e:
            message = '保存按钮定位失败'
            LOG_ERROR("error: found save button timeout")
            print(e)
            LOG_INFO(message)

    def cancel_button(self):
        """
        配置系统参数页面取消按钮
        """
        try:
            self.driver.find_element(By.XPATH, '//*[@id="right"]/div/div/div/div[1]/div[2]/button[2]').click()
        except Exception as e:
            message = '取消按钮定位失败'
            LOG_ERROR("error: found cancel button timeout")
            print(e)
            LOG_INFO(message)

    def config_system_UCT_time(self, pipeline_type):
        """
        配置系统参数页面uct time下拉框
        """
        try:
            UTC_time = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                          '2]/section/section/main/div/div/div/form/div['
                                                          '3]/div/div/div/div/div/input')
            UTC_time.click()
            time.sleep(2)
            if pipeline_type == "version_iso":
                loc1 = (By.XPATH, '/html/body/div[2]/div[3]/div/div/div[1]/ul/li[2]')
                WebDriverWait(self.driver, 10).until(expected_conditions.visibility_of_element_located(loc1))
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[3]/div/div/div[1]/ul/li[2]').click()
            if pipeline_type == "image_iso":
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[7]/div/div/div[1]/ul/li[2]').click()
            time.sleep(2)
            value1 = UTC_time.get_attribute("value")
            UTC_time.click()
            time.sleep(2)
            if pipeline_type == "version_iso":
                loc2 = (By.XPATH, '/html/body/div[2]/div[3]/div/div/div[1]/ul/li[1]')
                WebDriverWait(self.driver, 10).until(expected_conditions.visibility_of_element_located(loc2))
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[3]/div/div/div[1]/ul/li[1]').click()
            if pipeline_type == "image_iso":
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[7]/div/div/div[1]/ul/li[1]').click()
            time.sleep(2)
            value2 = UTC_time.get_attribute("value")
            return [value1, value2]
        except Exception as e:
            message = 'uct time下拉框定位失败'
            LOG_ERROR("error: found uct time select timeout")
            print(e)
            LOG_INFO(message)

    def crop_rpm_package(self, pipeline_type):
        """
        配置系统参数页面裁剪rpm包下拉框
        """
        try:
            crop_rpm = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                          '2]/section/section/main/div/div/div/form/div[5]/div/div['
                                                          '1]/div/div/div/input')
            crop_rpm.click()
            time.sleep(2)
            if pipeline_type == "version_iso":
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[4]/div/div/div[1]/ul/li[2]').click()
            if pipeline_type == "image_iso":
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[8]/div/div/div[1]/ul/li[2]').click()
            time.sleep(2)
            value1 = crop_rpm.get_attribute("value")
            crop_rpm.click()
            time.sleep(2)
            if pipeline_type == "version_iso":
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[4]/div/div/div[1]/ul/li[1]').click()
            if pipeline_type == "image_iso":
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[8]/div/div/div[1]/ul/li[1]').click()
            time.sleep(2)
            value2 = crop_rpm.get_attribute("value")
            return [value1, value2]
        except Exception as e:
            message = '裁剪rpm下拉框定位失败'
            LOG_ERROR("error: found crop rpm package select timeout")
            print(e)
            LOG_INFO(message)

    def crop_user_add_rpm(self, pipeline_type):
        """
        配置系统参数页面裁剪用户添加的rpm包下拉框
        """
        try:
            crop_user_rpm = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                               '2]/section/section/main/div/div/div/form/div['
                                                               '6]/div/div[1]/div/div/div/input')
            crop_user_rpm.click()
            time.sleep(3)
            if pipeline_type == "version_iso":
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[5]/div/div/div[1]/ul/li[2]').click()
            if pipeline_type == "image_iso":
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[9]/div/div/div[1]/ul/li[2]').click()
            if pipeline_type == "cpio":
                self.driver.find_element(By.XPATH, '').click()
            time.sleep(2)
            value1 = crop_user_rpm.get_attribute("value")
            crop_user_rpm.click()
            time.sleep(3)
            if pipeline_type == "version_iso":
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[5]/div/div/div[1]/ul/li[1]').click()
            if pipeline_type == "image_iso":
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[9]/div/div/div[1]/ul/li[1]').click()
            if pipeline_type == "cpio":
                self.driver.find_element(By.XPATH, '').click()
            time.sleep(2)
            value2 = crop_user_rpm.get_attribute("value")
            return [value1, value2]
        except Exception as e:
            message = '裁剪用户添加的rpm包下拉框定位失败'
            LOG_ERROR("error: found crop users to add rpm packages select timeout")
            print(e)
            LOG_INFO(message)

    def crop_man_document(self, pipeline_type):
        """
        配置系统参数页面裁剪man文档下拉框
        """
        try:
            man_document = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                              '2]/section/section/main/div/div/div/form/div['
                                                              '10]/div/div[1]/div/div/div/input')
            man_document.click()
            time.sleep(3)
            if pipeline_type == "version_iso":
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[7]/div/div/div[1]/ul/li[2]').click()
            if pipeline_type == "image_iso":
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[11]/div/div/div[1]/ul/li[2]').click()
            if pipeline_type == "cpio":
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[11]/div/div/div[1]/ul/li[1]').click()
            time.sleep(3)
            value1 = man_document.get_attribute("value")
            man_document.click()
            time.sleep(3)
            if pipeline_type == "version_iso":
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[7]/div/div/div[1]/ul/li[1]').click()
            if pipeline_type == "image_iso":
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[11]/div/div/div[1]/ul/li[1]').click()
            if pipeline_type == "cpio":
                self.driver.find_element(By.XPATH, '').click()
            time.sleep(3)
            value2 = man_document.get_attribute("value")
            return [value1, value2]
        except Exception as e:
            message = '裁剪man文档下拉框定位失败'
            LOG_ERROR("error: found crop man documents select timeout")
            print(e)
            LOG_INFO(message)

    def host_name_inputbox(self):
        """
        配置系统参数页面主机名输入框
        """
        try:
            self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                               '2]/section/section/main/div/div/div/form/div[7]/div/div['
                                               '1]/div/input').send_keys('openEuler_')
            time.sleep(2)
            illegal_input = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                               '2]/section/section/main/div/div/div/form/div['
                                                               '7]/div/div[2]')
            return illegal_input.text
        except Exception as e:
            message = '主机名输入框定位失败'
            LOG_ERROR("error: found host name inputbox timeout")
            print(e)
            LOG_INFO(message)

    def host_name_nomal_inputbox(self):
        """
        配置系统参数页面主机名输入框正常输入
        """
        try:
            self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                               '2]/section/section/main/div/div/div/form/div[7]/div/div['
                                               '1]/div/input').send_keys('openeuler')
            time.sleep(2)
        except Exception as e:
            message = '主机名输入框定位失败'
            LOG_ERROR("error: found host name inputbox timeout")
            print(e)
            LOG_INFO(message)

    def password_illegal_input(self):
        # 密码输入框
        try:
            password = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                          '2]/section/section/main/div/div/div/form/div[12]/div/div['
                                                          '1]/div/input')
            grub_password = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                               '2]/section/section/main/div/div/div/form/div[14]/div['
                                                               '2]/div/div[1]/div/input')
            password.send_keys('12345678')
            grub_password.send_keys('12345678')
            time.sleep(5)
            self.save_button()
            time.sleep(1)
            confirm_password_text = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                                       '2]/section/section/main/div/div/div/form/div['
                                                                       '12]/div/div[2]').text
            confirm_grub_password_text = self.driver.find_element(By.XPATH,
                                                                  '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                                  '2]/section/section/main/div/div/div/form'
                                                                  '/div[14]/div[2]/div/div[2]').text
            return [confirm_password_text, confirm_grub_password_text]
        except Exception as e:
            message = '密码输入框定位失败'
            LOG_ERROR("error: found password inputbox timeout")
            print(e)
            LOG_INFO(message)

    def password_short_input(self):
        # 密码输入框
        try:
            password = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                          '2]/section/section/main/div/div/div/form/div[12]/div/div['
                                                          '1]/div/input')
            grub_password = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                               '2]/section/section/main/div/div/div/form/div[14]/div['
                                                               '2]/div/div[1]/div/input')
            password.send_keys('123456')
            grub_password.send_keys('123456')
            time.sleep(5)
            self.save_button()
            time.sleep(1)
            confirm_password_text = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                                       '2]/section/section/main/div/div/div/form/div['
                                                                       '12]/div/div[2]').text
            confirm_grub_password_text = self.driver.find_element(By.XPATH,
                                                                  '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                                  '2]/section/section/main/div/div/div/form'
                                                                  '/div[14]/div[2]/div/div[2]').text
            return [confirm_password_text, confirm_grub_password_text]
        except Exception as e:
            message = '主机名输入框定位失败'
            LOG_ERROR("error: found host name inputbox timeout")
            print(e)
            LOG_INFO(message)

    def password_normal_input(self):
        # 密码输入框
        try:
            password = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                          '2]/section/section/main/div/div/div/form/div[12]/div/div['
                                                          '1]/div/input')
            confirm_password = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                                  '2]/section/section/main/div/div/div/form/div['
                                                                  '13]/div/div[1]/div/input')
            grub_password = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                               '2]/section/section/main/div/div/div/form/div[14]/div['
                                                               '2]/div/div[1]/div/input')

            confirm_grub_password = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div['
                                                                       '2]/section/section/main/div/div/div/form/div['
                                                                       '14]/div[3]/div/div[1]/div/input')

            password.send_keys('openEuler12#$')
            confirm_password.send_keys('openEuler12#$')
            grub_password.send_keys('openEuler12#$')
            confirm_grub_password.send_keys('openEuler12#$')
            time.sleep(1)

        except Exception as e:
            message = '主机名输入框定位失败'
            LOG_ERROR("error: found host name inputbox timeout")
            print(e)
            LOG_INFO(message)

    def building_image(self):
        # 制作镜像按钮
        try:
            self.driver.find_element(By.XPATH,
                                     '/html/body/div[1]/div[1]/div/div[2]/div/div[2]/section/footer/button[1]').click()

        except Exception as e:
            message = '制作镜像按钮定位失败'
            LOG_ERROR("error: found building image button timeout")
            print(e)
            LOG_INFO(message)

    def confirm_building_image(self):
        # 制作镜像确认按钮
        try:
            self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/div[2]/div['
                                               '2]/div/div/footer/div/span/button[1]').click()

        except Exception as e:
            message = '制作镜像确认按钮定位失败'
            LOG_ERROR("error: found building confirm image button timeout")
            print(e)
            LOG_INFO(message)


